<?php
/*
 * @Descripttion: 系统提供的消息队列服务--推送消息到队列测试范例
 * @version:
 * @Author: ewgof
 * @Date: 2020-08-12 00:24:23
 * @LastEditors: ewgof
 * @LastEditTime: 2023-02-24 10:13:38
*/

use Workerman\Worker;
use cw\facade\Queue;
use Workerman\Lib\Timer;
use app\Test;

$redis = null;
global $redis;

$queue_worker = new Worker();
$queue_worker->name = 'queue-test';

$queue_worker->onConnect = function ($connection) {
};

$queue_worker->onWorkerStart = function ($connection) {
    echo '队列测试-推送消息到队列服务的服务启动了 。。。 ' . PHP_EOL;
    // 正常推送消息到队列 示例
    
    Timer::add(3, function() {
        $handler = [ Test::class, 'pullHandle' ];
        $params['msg'] = '这是测试队列服务的消息~1';
        $retry_times = 3;
        $queue_name = '';
        $init_params = [];
        Queue::connect();
        Queue::push($handler, $params, $retry_times, $queue_name, $init_params);
    }, [], false);
   


    // 测试 获取队列信息的 info 方法
    /*
    $handler = [Test::class, 'pullHandle'];
    $params['msg'] = '这是测试队列服务的消息~1';
    $retry_times = 3;
    $queue_name = '';
    $init_params = [];
    Queue::connect();
    Queue::push($handler, $params, $retry_times, $queue_name, $init_params);

    $params['msg'] = '这是测试队列服务的消息~2';
    Queue::push($handler, $params, $retry_times, $queue_name, $init_params);

    $queue_name = 'test:13131:jjjj';
    $params['msg'] = '这是测试队列服务的消息~3';
    Queue::push($handler, $params, $retry_times, $queue_name, $init_params);

    $params['msg'] = '这是测试队列服务的消息~4';
    Queue::push($handler, $params, $retry_times, $queue_name, $init_params);

    $params['msg'] = '这是测试队列服务的消息~5';
    $queue_name = 'message:queue:retry';
    Queue::push($handler, $params, $retry_times, $queue_name, $init_params);

    Timer::add(3, function () {
        $res = queue_info();
        var_dump($res);
    }, [], false);
    */

    // 抢占式推送消息到队列 示例
    // Timer::add(3, function() {
    //     echo '抢占式推送消息到队列' . PHP_EOL;
    //     $handler = [ Test::class, 'pullHandle' ];
    //     $params['msg'] = '这是测试队列服务的消息~';
    //     $retry_times = 3;
    //     $init_params = [];
    //     seizePush($handler, $params, $retry_times, $init_params);
    // }, [], false);
};


$queue_worker->onMessage = function ($connection, $msg) {
};


$queue_worker->onWorkerStop = function ($connection) {
};


$queue_worker->onClose = function ($connection) {
};

// 如果不是在根目录启动，则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}
